<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="主标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="form.title" placeholder="请输入主标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公告展示图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrl">
              <img :src="getImgView(form.imgUrl)" height="55px" alt=""
                   style="max-width:80px;font-size: 12px;font-style: italic;" v-if="isLook"/>
              <j-image-upload v-model="form.imgUrl" v-else></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="form.content" rows="4" placeholder="请输入副标题"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="推送类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pushType">
              <j-dict-select-tag type="list" v-model="form.pushType" dictCode="notice_push_type" placeholder="请选择推送类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="form.pushType == 1">
            <a-form-model-item label="单推用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pushType">
              <a-tag v-for="(item, index) in members" :key="index" closable @close="deleteItem(index)">
                {{ item }}
              </a-tag>
              <a-button type="primary" @click="choiceMember">选 择</a-button>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sendStatus">
              <j-dict-select-tag type="list" v-model="form.sendStatus" dictCode="notice_send_status"
                                 placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="展现方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="form.type" dictCode="notice_click_type" placeholder="请选择展现方式"
                                 @change="form.param=undefined"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="form.type == 7">
            <a-form-model-item label="跳转链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="param">
              <a-input v-model="form.param" placeholder="请输入跳转链接"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="form.type == 6">
            <a-form-model-item label="富文本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentEdit">
              <j-dict-select-tag type="list" v-model="form.param" dictCode="cms_floor_content,title,id, del_flag = '0'"
                                 placeholder="请选择富文本"/>
            </a-form-model-item>
          </a-col>
          <ums-member-ref ref="modalMember" :selectionType="'checkbox'" @ok="modalMemberOk"/>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction, getFileAccessHttpUrl} from '@/api/manage'
import pick from 'lodash.pick'
import {validateDuplicateValue} from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JDictSelectTag from "@/components/dict/JDictSelectTag"
import UmsMemberRef from './UmsMemberRef'

export default {
  name: 'CmsNoticeTempletForm',
  components: {
    JFormContainer,
    JDate,
    JImageUpload,
    JDictSelectTag,
    UmsMemberRef,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: {
        title: '',
        content: '',
        imgUrl: '',
        createTime: '',
        updateTime: '',
        sendStatus: 0,
        type: 0,
        param: '',
        delFlag: '',
        remark: '',
        nicknames: '',
        pushType: 0,
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      rules: {
        title: [
          {required: true, message: '请输入主标题!'},
        ],
        param: [
          {required: true, message: '请输入连接信息!'},
        ],
        content: [
          {required: true, message: '请输入副标题!', trigger: 'blur'},
          {min: 2, max: 100, message: "副标题请不要超过100个字", trigger: 'blur'}
        ],
        imgUrl: [
          {required: true, message: '请输入公告展示图!'},
        ],
        sendStatus: [
          {required: true, message: '请选择状态!'},
        ],
        type: [
          {required: true, message: '请选择展现方式!'},
        ],
        pushType: [
          {required: true, message: '请选择推送类型!'},
        ],
        contentEdit: [
          {required: true, message: '请选择富文本信息!'},
        ],
      },
      url: {
        add: "/common/cmsNoticeTemplet/add",
        edit: "/common/cmsNoticeTemplet/edit",
        queryById: "/common/cmsNoticeTemplet/queryById"
      },
      members: [],
      isLook: false,
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    getImgView(text) {
      if (text && text.indexOf(",") > 0) {
        text = text.substring(0, text.indexOf(","))
      }
      return getFileAccessHttpUrl(text)
    },
    deleteItem(index) {
      this.members.splice(index, 1)
      this.form.nicknames = ''

      this.members.forEach(item => {
        if (this.form.nicknames == '') {
          this.form.nicknames = item
        } else {
          this.form.nicknames = this.form.nicknames + "," + item
        }
      })
    },
    add() {
      this.edit({
        title: '',
        content: '',
        imgUrl: '',
        createTime: '',
        updateTime: '',
        sendStatus: undefined,
        type: undefined,
        param: '',
        delFlag: '',
        remark: '',
        nicknames: '',
        pushType: undefined,
      })
    },
    edit(record) {
      this.members = []
      if (record.nicknames) {
        this.members = record.nicknames.split(",")
      }
      this.form = Object.assign({}, record)
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId}
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.form.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback(row) {
      this.form = row
    },
    choiceMember() {
      const self = this
      self.$refs.modalMember.visible = true
      if (this.form.remark) {
        self.$refs.modalMember.checkedKeys = this.form.remark.split(",")
      } else {
        self.$refs.modalMember.checkedKeys = []
      }
      self.$refs.modalMember.open()
    },
    modalMemberOk(rows, keys) {
      const self = this
      self.form.remark = keys.join(',')
      self.members = []
      self.form.nicknames = ''
      console.log(rows)
      if (rows) {
        rows.forEach(item => {

          if (item) {
            self.members.push(item.nickname ? item.nickname : '未命名')
            if (self.form.nicknames == '') {
              self.form.nicknames = (item.nickname ? item.nickname : '未命名')
            } else {
              self.form.nicknames = self.form.nicknames + "," + (item.nickname ? item.nickname : '未命名')
            }
          }
          console.log(self.form.nicknames)

        })
      }

    },
  }
}
</script>
